/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/26
 * Time: 11:16
 */
$(function(){
    var authorization = Cookies.get('_token');
    var page = 1;
    var page_size = 10;
    var data = {
        page: page,
        page_size: page_size,
        order: 'desc'
    };
    getDatas(authorization, data);
    $(document).on('click', '.paging_simple_numbers a', function(){
        data.page = $(this).attr('data-page');
        getDatas(authorization, data)
    });
    $(document).on('click', '.btn-delete', function(){
        var params = {id: $(this).attr('data-id')};
        $.ajax({
            type: 'post',
            url: '/v1/manager/deleteUser',
            headers: {
                Authorization: authorization,
                Accept: "application/json; charset=utf-8"
            },
            data: {params: params},
            success: function(e){
                var data = JSON.parse(e);
                if (data.status == 200) {
                    alert('删除成功!');
                    location.reload();
                }
            }
        });
    });
});

function getDatas(authorization, data) {
    $.ajax({
        type: 'get',
        url: '/v1/manager/user/list.json',
        headers: {
            Authorization: authorization,
            Accept: "application/json; charset=utf-8"
        },
        data: data,
        success: function(e){
            var data = JSON.parse(e);
            if (data.status == 200) {
                var html = '<tr class="no-data">';
                html += '<td colspan="8" class="text-center">暂无数据</td>';
                html += '</tr>';
                var json = data.body.data;
                if (json.rows.length > 0) {
                    html = '<tr class="no-data hidden">';
                    html += '<td colspan="8" class="text-center">暂无数据</td>';
                    html += '</tr>';
                    $.each(json.rows, function(n, v){
                        var role = '';
                        switch (v.role) {
                            case 1: role = '学生';break;
                            case 2: role = '教师';break;
                            case 3: role = '超级管理员';break;
                            default: role = '学生';break;
                        }
                        html += '<tr>';
                        html += '<td>'+v.id+'</td>';
                        html += '<td>'+v.username+'</td>';
                        html += '<td>'+v.realname+'</td>';
                        html += '<td>'+v.number+'</td>';
                        html += '<td>'+v.email+'</td>';
                        html += '<td>'+role+'</td>';
                        html += '<td>'+v.university+'</td>';
                        html += '<td>'+v.college+'</td><td>';
                        if (v.role == 2) {
                            html += '<a class="btn btn-primary btn-xs" href="/manager/user-edit/'+v.id+'.html">编辑</a>&nbsp;&nbsp;';
                        }
                        html += '<a class="btn btn-danger btn-xs btn-delete" data-id="'+v.id+'" href="javascript:;">删除</a></td></tr>';
                    });
                }
                $('tr.no-data').parent().html('').append(html);

                var page = '<div class="row">';
                page += '<div class="col-sm-6">';
                if (json.rows.length > 0) {
                    page += '<div class="dataTables_info" role="alert" aria-live="polite" aria-relevant="all">显示 <span class="data-from">'+(json.offset+1)+'</span> 到 <span class="data-to">'+((json.page_number-1)*json.page_size+json.rows.length)+'</span> 项，共 <span class="data-total">'+json.total+'</span> 项</div>';
                }
                page += '</div>';
                if (json.pages > 1) {
                    page += '<div class="col-sm-6">';
                    page += '<div class="dataTables_paginate paging_simple_numbers">';
                    page += '<ul class="pagination" role="navigation">';

                    if (json.page_number == 1) {
                        page += '<li class="page-item disabled" aria-disabled="true" aria-label="pagination.previous"><span class="page-link" aria-hidden="true">‹</span></li>';
                    } else {
                        page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+(json.page_number-1)+'" rel="previous" aria-label="pagination.previous">‹</a></li>';
                    }
                    for (var k = 1; k <= json.pages; k ++) {
                        if (k == json.page_number) {
                            page += '<li class="page-item active" aria-current="page"><span class="page-link">'+k+'</span></li>';
                        } else {
                            page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+k+'">'+k+'</a></li>';
                        }
                    }

                    if (json.page_number < json.pages) {
                        page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+(json.page_number+1)+'" rel="next" aria-label="pagination.next">›</a></li>';
                    } else {
                        page += '<li class="page-item disabled" aria-disabled="true" aria-label="pagination.next"><span class="page-link" aria-hidden="true">›</span></li>';
                    }

                    page += '</ul>';
                    page += '</div>';
                    page += '</div>';
                }
                page += '</div>';
                $('.user-list .row').remove();
                $('.user-list').append(page);
            }
        }
    });
}